﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Steamrunner
{
    class PlayerSelectScreen : Screen
    {
        Texture2D background = null;
        Texture2D title = null;

        string nextScreen;

        public Rectangle screenBoundaries;

        IniFile config;

        public PlayerSelectScreen(ScreenManager screenManager, Game game)
            : base(screenManager, game)
        {
        }

        public override void Initialize()
        {
            config = new IniFile("config.ini");
            config.Parse();
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            screenBoundaries = Game.Window.ClientBounds;

            background = Game.Content.Load<Texture2D>(@"Images\Menus\background");
            title = Game.Content.Load<Texture2D>(@"Images\Menus\ps_title");

            guiManager = new GuiManager(Game);

            Button button = new Button(Game, Spritebatch, "Images\\Menus\\ps_1p", new Vector2(screenBoundaries.Width * 0.245f, screenBoundaries.Height * 0.15f), "1");
            guiManager.AddButton(button);

            button = new Button(Game, Spritebatch, "Images\\Menus\\ps_2p", new Vector2(screenBoundaries.Width * 0.245f, screenBoundaries.Height * 0.3f), "2");
            guiManager.AddButton(button);

            button = new Button(Game, Spritebatch, "Images\\Menus\\ps_3p", new Vector2(screenBoundaries.Width * 0.245f, screenBoundaries.Height * 0.45f), "3");
            guiManager.AddButton(button);

            button = new Button(Game, Spritebatch, "Images\\Menus\\ps_4p", new Vector2(screenBoundaries.Width * 0.245f, screenBoundaries.Height * 0.6f), "4");
            guiManager.AddButton(button);

            button = new Button(Game, Spritebatch, "Images\\Menus\\ps_btmm", new Vector2(screenBoundaries.Width * 0.245f, screenBoundaries.Height * 0.8f), "MenuScreen");
            guiManager.AddButton(button);

            guiManager.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            musicManager.Update(gameTime);

            inputManager.Update(gameTime);

            if (inputManager.JustPressed(Keys.W))
            {
                guiManager.JoystickUp();
                musicManager.PlaySound("buttonScrolling");
            }
            else if (inputManager.JustPressed(Keys.S))
            {
                guiManager.JoystickDown();
                musicManager.PlaySound("buttonScrolling");
            }

            if (inputManager.JustPressed(Keys.A) || inputManager.JustPressed(Keys.D))
            {
                nextScreen = guiManager.ButtonClicked();
                musicManager.PlaySound("buttonSelect");
                if (nextScreen == "MenuScreen")
                {
                    screenManager.CurrentScreen = nextScreen;
                }
                else
                {
                    int playerAmount = Int32.Parse(nextScreen);
                    config.AddModify("Players", "Amount", (float)playerAmount);
                    config.Save();
                    musicManager.StopMusic();
                    nextScreen = "PlayScreen";
                    screenManager.CurrentScreen = nextScreen;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Spritebatch.Begin();

            Spritebatch.Draw(background, Vector2.Zero, Color.White);

            Spritebatch.Draw(title, Vector2.Zero, Color.White);

            guiManager.Draw(gameTime);

            Spritebatch.End();

            base.Draw(gameTime);
        }

        public override string GetNextScreen()
        {
            return nextScreen;
        }
    }
}
